using AdvancedInspector;
using UnityEngine;

[AdvancedInspector(true)]
public class ChestButton : MonoBehaviour
{
	public string idChest;

	public UITexture iconItem;

	public UILabel timeOrCountLabel;

	public UITexture itemTexture;

	public UITexture itemTextureMany;

	public ChestBonusItem purchaseInfo;

	private InAppProduct curProd;

	public void Initialize()
	{
		ChestBonusController.OnChestBonusChange += CheckBonusButtonUpdate;
	}

	public void UpdateState()
	{
		if (ChestBonusController.instance == null || !ChestBonusController.instance.IsBonusActive)
		{
			base.gameObject.SetActive(false);
			return;
		}
		if (curProd == null)
		{
			curProd = GetComponentInParent<InAppProduct>();
		}
		CheckBonusButtonUpdate();
	}

	public void Deinitialize()
	{
		ChestBonusController.OnChestBonusChange -= CheckBonusButtonUpdate;
		purchaseInfo = null;
	}

	private void CheckBonusButtonUpdate()
	{
		purchaseInfo = ChestBonusController.instance.GetChestById(idChest);
		bool flag = purchaseInfo != null && purchaseInfo.isVisible;
		base.gameObject.SetActive(flag);
		if (flag)
		{
			SetViewData(purchaseInfo);
		}
	}

	private void SetViewData(ChestBonusItem purchaseInfo)
	{
		timeOrCountLabel.text = purchaseInfo.GetItemCountOrTime();
		Texture image = purchaseInfo.GetImage();
		if (image != null)
		{
			itemTexture.gameObject.SetActive(true);
			itemTextureMany.gameObject.SetActive(false);
			itemTexture.mainTexture = image;
		}
		else
		{
			itemTexture.gameObject.SetActive(false);
			itemTextureMany.gameObject.SetActive(true);
		}
	}

	public void OnButtonClick()
	{
		ChestBonusController.instance.ShowBonusWindowForItem(purchaseInfo, iconItem.mainTexture, curProd);
	}
}
